### Rachel Porter
### 12/1/2022
### Replication file for main manuscript figures

rm(list=ls())

#load libraries
library(ggplot2)

# clear environment and set working directory
setwd("~/Dropbox/Primary_Elections/Issue Paper/Replication_Archive/JOP_Replication_Files")

#################### 
#### FIGURE 2
#################### 

# loading data 
female <- readRDS("figure_2_female.rds")
race <- readRDS("figure_2_race.rds")

# binding data 
master <- rbind.data.frame(female, race)
colnames(master) <- c("pe", "lo", "hi")

# adding candidate type column 
master$type <- c("Professional", "Amateur", "Professional", "Amateur")
master$type <- factor(master$type, levels = c("Professional", "Amateur"))

# adding competitor type column
master$competitor <- rep(c("Effect of Female Candidate Presence on\nMale Candidate Adoption of Women's Issues", "Effect of Black Candidate Presence on\nWhite Candidate Adoption of Black-Associated Issues"), each = 2)

master$competitor <- factor(master$competitor, levels = c("Effect of Female Candidate Presence on\nMale Candidate Adoption of Women's Issues", "Effect of Black Candidate Presence on\nWhite Candidate Adoption of Black-Associated Issues"))

p <- ggplot(master, aes(x = type, y = pe)) +
  geom_point(size=3) +
  geom_errorbar(aes(ymin=lo, ymax=hi), width=.1)+
  geom_hline(yintercept=c(0), linetype="dashed", size = 0.5) +
  scale_y_continuous(limits = c(-.1, .40), breaks = c(-.1, 0, .1, .2, .3, .4)) +
  ylab("First Differences")+
  xlab("\nType of Male / White Candidate") +
  theme_bw()

p + facet_grid(cols = vars(competitor))+
  theme(strip.text.x = element_text(size = 15), strip.text.y = element_text(size = 15),
        text = element_text(size=20))

rm(list=ls())
#################### 
#### FIGURE 3
#################### 

# loading data 
female <- readRDS("figure_3_female.rds")
race <- readRDS("figure_3_race.rds")

# binding data 
master <- rbind.data.frame(female, race)
colnames(master) <- c("pe", "lo", "hi")

# adding candidate type column 
master$issue <-c("No Substantive\nCoverage", "Substantive\nCoverage", 
                 "No Substantive\nCoverage", "Substantive\nCoverage")
master$issue <- factor(master$issue, levels = c("Substantive\nCoverage", 
                                                "No Substantive\nCoverage"))

# adding competitor type column
master$competitor <- rep(c("Effect of Female Candidate Presence on\nMale Candidate Coverage of Women's Issues", "Effect of Black Candidate Presence on\nWhite Candidate Coverage of Black-Associated Issues"), each = 2)

master$competitor <- factor(master$competitor, levels = c("Effect of Female Candidate Presence on\nMale Candidate Coverage of Women's Issues", "Effect of Black Candidate Presence on\nWhite Candidate Coverage of Black-Associated Issues"))

p <- ggplot(master, aes(x = issue, y = pe)) +
  geom_point(size=3) +
  geom_errorbar(aes(ymin=lo, ymax=hi), width=.1)+
  geom_hline(yintercept=c(0), linetype="dashed", size = 0.5) +
  scale_y_continuous(limits = c(-.05, .45), breaks = c(0, .1, .2, .3, .4)) +
  ylab("First Differences")+
  xlab("\nFemale/Black Candidate Coverage of Identity-Related Issues") +
  theme_bw()

p + facet_grid(cols = vars(competitor))+
  theme(strip.text.x = element_text(size = 15), strip.text.y = element_text(size = 15),
        text = element_text(size=20))

rm(list=ls())
#################### 
#### FIGURE 4
#################### 

# loading data 
female <- readRDS("figure_4_female.rds")
race <- readRDS("figure_4_race.rds")

# binding data 
master <- rbind.data.frame(female, race)
colnames(master) <- c("pe", "lo", "hi")

# adding candidate type column 
master$issue <-c("No Substantive\nCoverage", "Substantive\nCoverage", 
                 "No Substantive\nCoverage", "Substantive\nCoverage")
master$issue <- factor(master$issue, levels = c("Substantive\nCoverage", 
                                                "No Substantive\nCoverage"))

# adding competitor type column
master$competitor <- rep(c("Effect of Female Candidate Presence on\nMale Candidate Coverage of Women's Issues", "Effect of Black Candidate Presence on\nWhite Candidate Coverage of Black-Associated Issues"), each = 2)

master$competitor <- factor(master$competitor, levels = c("Effect of Female Candidate Presence on\nMale Candidate Coverage of Women's Issues", "Effect of Black Candidate Presence on\nWhite Candidate Coverage of Black-Associated Issues"))

p <- ggplot(master, aes(x = issue, y = pe)) +
  geom_point(size=3) +
  geom_errorbar(aes(ymin=lo, ymax=hi), width=.1)+
  geom_hline(yintercept=c(0), linetype="dashed", size = 0.5) +
  scale_y_continuous(limits = c(-.15, .31), breaks = c(-.1, 0, .1, .2, .3)) +
  ylab("First Differences")+
  xlab("\nFemale/Black Candidate Coverage of Identity-Related Issues") +
  theme_bw()

p + facet_grid(cols = vars(competitor))+
  theme(strip.text.x = element_text(size = 15), strip.text.y = element_text(size = 15),
        text = element_text(size=20))

rm(list=ls())
#################### 
#### FIGURE 5
#################### 

# loading data 
reps <- readRDS("figure_5_rvet.rds")
dems <- readRDS("figure_5_dvet.rds")

# binding data 
master <- rbind.data.frame(reps, dems)
colnames(master) <- c("pe", "lo", "hi")

# adding candidate type column 
master$type <- c("Professional", "Amateur", "Professional", "Amateur")
master$type <- factor(master$type, levels = c("Professional", "Amateur"))

# adding competitor type column
master$competitor <- rep(c("Effect of  Military Veteran Presence on\nNon-Veteran Issue Adoption in Republican Primaries","Effect of Military Veteran Presence on\nNon-Veteran Issue Adoption in Democratic Primaries"), each = 2)

master$competitor <- factor(master$competitor, levels = c("Effect of  Military Veteran Presence on\nNon-Veteran Issue Adoption in Republican Primaries", "Effect of Military Veteran Presence on\nNon-Veteran Issue Adoption in Democratic Primaries"))

p <- ggplot(master, aes(x = type, y = pe)) +
  geom_point(size=3) +
  geom_errorbar(aes(ymin=lo, ymax=hi), width=.1)+
  geom_hline(yintercept=c(0), linetype="dashed", size = 0.5) +
  scale_y_continuous(limits = c(-.1, .3), breaks = c(-0.1, 0, .1, .2, .3)) +
  ylab("First Differences")+
  xlab("\nType of Republican / Democrat Non-Veteran") +
  theme_bw()

p + facet_grid(cols = vars(competitor))+
  theme(strip.text.x = element_text(size = 15), strip.text.y = element_text(size = 15),
        text = element_text(size=20))

rm(list=ls())
#################### 
#### FIGURE 6
#################### 

# loading data 
master <- readRDS("figure_6_barplot.rds")

# calculating percent for each category by type 
men <- subset(master, master$type == "male")
men <- c(sum(men$added_text) / nrow(men), sum(men$subtracted_text) / 
            nrow(men), sum(men$changed_text) / nrow(men))
white <- subset(master, master$type == "white")
white <- c(sum(white$added_text) / nrow(white), sum(white$subtracted_text) / 
            nrow(white), sum(white$changed_text) / nrow(white))

candidates <- c(men, white)

# adding descriptors 
winner <- rep(c("Male Democratic\nPrimary Election Winner", 
                "White Democratic\nPrimary Election Winner"), each = 3)
type <- rep(c("No Change or\nAdded Text", 
                "Change in Content or\n Change in Structure", 
                "Removed or\nShortened Text"), times = 2)

# binding for plotting 
master <- cbind.data.frame(candidates, type, winner)
master$type <- factor(master$type, 
                      levels = c("No Change or\nAdded Text", 
                                 "Change in Content or\n Change in Structure", 
                                 "Removed or\nShortened Text"))

p <- ggplot(data=master, aes(x=type, y=candidates)) +
  geom_bar(stat="identity", position=position_dodge())+
  theme_minimal()+
  ylim(0,1) +
  theme_bw()+
  scale_fill_grey(start = 0, end = .9) +
  labs(fill = "") +
  theme(legend.position = "bottom") +
  ylab("\n % of Primary Election Winners")+
  xlab("\nText from Primary to General Election")+
  theme(text = element_text(size=18)) 

p + facet_grid(cols = vars(winner))+
  theme(strip.text.x = element_text(size = 25))
